1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<!DOCTYPE html>
<html>
<head>
<title>我的网页</title>
</head>
<body>
<script type="module">
const PI = 3.141592653589793238;
import init from './pkg/pi_orca.js';
import { RVOSimulator } from './pkg/pi_orca.js';
const temp = 100;
const num = 50;
const remove_id = num / 2;
const maxSpeed = 5;
let lastpotion = [];
let is_remove = false;
class Vector2 {
constructor(obj) {
this.x = obj.x;
this.y = obj.y;
}
static new(x, y) {
return new Vector2({ x, y });
}
sub(other) {
return Vector2.new(this.x - other.x, this.y - other.y);
}
mul_number(other) {
return Vector2.new(this.x * other, this.y * other);
}
static abs_sq(other) {
return other.x * other.x + other.y * other.y;
}
static normalize(other) {
let length = Math.sqrt(Vector2.abs_sq(other));
return Vector2.new(other.x / length, other.y / length);
}
}
// 0
function setup_scenario(sim, goals, divs, agents) {
sim.set_time_step(0.25);
sim.set_agent_defaults(15, 10, 10.0, 10.0, 2.5, maxSpeed, 0.0, 0.0);
// for (let i = 0; i < num; i++) {
let x = 50;
let y = 50;
divs[0] = document.createElement("div");
divs[0].style.left = Math.floor(x + temp) + "px";
divs[0].style.top = Math.floor(y + temp) + "px";
divs[0].style.width = "4px";
divs[0].style.height = "4px";
divs[0].style.borderRadius = "50%";
divs[0].style.backgroundColor = "rgb(1, 0, 0)";
// console.log(Math.floor(1 / num * 256));
divs[0].style.position = "absolute";
divs[0].id = "div" + 0;
document.body.appendChild(divs[0]);
agents[0] = sim.add_agent(100, 0, maxSpeed);
// console.log("-sim.getAgentPosition(i): ", sim.get_agent_position(i).neg());
sim.set_agent_goal(agents[0], 0, 0);
// sim.set_agent_velocity(agents[0], -5, 0);
divs[1] = document.createElement("div");
divs[1].style.left = Math.floor(x + temp) + "px";
divs[1].style.top = Math.floor(y + temp) + "px";
divs[1].style.width = "4px";
divs[1].style.height = "4px";
divs[1].style.borderRadius = "50%";
divs[1].style.backgroundColor = "rgb(1, 0, 0)";
// console.log(Math.floor(1 / num * 256));
divs[1].style.position = "absolute";
divs[1].id = "div" + 1;
document.body.appendChild(divs[1]);
agents[1] = sim.add_agent(120, 0, maxSpeed);
// console.log("-sim.getAgentPosition(i): ", sim.get_agent_position(i).neg());
sim.set_agent_goal(agents[1], -20, 0);
// divs[2] = document.createElement("div");
// divs[2].style.left = Math.floor(x + temp) + "px";
// divs[2].style.top = Math.floor(y + temp) + "px";
// divs[2].style.width = "4px";
// divs[2].style.height = "4px";
// divs[2].style.borderRadius = "50%";
// divs[2].style.backgroundColor = "rgb(1, 0, 0)";
// // console.log(Math.floor(1 / num * 256));
// divs[2].style.position = "absolute";
// divs[2].id = "div" + 2;
// document.body.appendChild(divs[2]);
// agents[2] = sim.add_agent(-100, 0, maxSpeed);
// // console.log("-sim.getAgentPosition(i): ", sim.get_agent_position(i).neg());
// sim.set_agent_goal(agents[2], 100, 0);
// divs[3] = document.createElement("div");
// divs[3].style.left = Math.floor(x + temp) + "px";
// divs[3].style.top = Math.floor(y + temp) + "px";
// divs[3].style.width = "4px";
// divs[3].style.height = "4px";
// divs[3].style.borderRadius = "50%";
// divs[3].style.backgroundColor = "rgb(1, 0, 0)";
// // console.log(Math.floor(1 / num * 256));
// divs[3].style.position = "absolute";
// divs[3].id = "div" + 3;
// document.body.appendChild(divs[3]);
// agents[3] = sim.add_agent(100, 0, maxSpeed);
// // console.log("-sim.getAgentPosition(i): ", sim.get_agent_position(i).neg());
// sim.set_agent_goal(agents[3], -100, 0);
// }
}
let last_position = [];
function update_visualization(sim, divs, agents) {
console.log("", sim.get_global_time());
let result = true;
for (let i = 0; i < agents.length; i++) {
if (i == remove_id && is_remove) {
continue;
}
let position = sim.get_agent_position(agents[i]);
// lastpotion[i] = position;
let velocity = sim.get_agent_velocity(agents[i]);
let pref_velocity = sim.get_agent_pref_velocity(agents[i]);
let x = Math.floor(position.x) + temp;
let y = Math.floor(position.y) + temp;
// console.log("Agent2: " + i + "; x: " + x + "; y: " + y);
divs[i].style.left = x + "px";
divs[i].style.top = y + "px";
// console.log("divs[i]: ", divs[i]);
// console.log("Agent: " + i + "; x: " + position.x + "; y: " + position.y);
let goal = sim.get_agent_goal(agents[i]);
console.log("当前位置: ", position);
console.log("目标位置: ", goal);
console.log("期望速度:", pref_velocity)
if (last_position.length != agents.length) {
last_position[i] = position;
} else {
let v_x = position.x - last_position[i].x;
let v_y = position.y - last_position[i].y;
console.log("当前速度:", { x: v_x, y: v_y })
// if (v_x == 0 && v_y == 0) {
// debugger
// }
last_position[i] = position;
}
if (goal) {
let max_speed = sim.get_agent_max_speed(agents[i]);
// console.log("max_speed: ", max_speed);
let custom_speed = sim.get_agent_custom_speed(agents[i]);
// console.log("custom_speed: ", custom_speed);
let speed = max_speed > custom_speed ? custom_speed : max_speed;
// console.log("speed: ", speed);
let position = sim.get_agent_position(agents[i]);
// console.log("position: ", position);
let dist_pos = Vector2.new(goal.x, goal.y).sub(Vector2.new(position.x, position.y));
// console.log("dist_pos: ", dist_pos);
let dist_sq = Vector2.abs_sq(dist_pos);
let velocity = Vector2.normalize(dist_pos).mul_number(speed * sim.get_time_step());
// console.log("velocity: ", velocity);
// console.log("Vector2.abs_sq(velocity): ", Vector2.abs_sq(velocity));
if (dist_sq > 0.001) {
// pref_velocity = velocity;
// 还未到达目标点
result = false;
} else {
// sim.set_agent_goal(agents[i], null, null);
}
}
console.log("");
}
if (result) {
console.log("到达目标点");
}
return result;
}
// function reached_goal(sim, goals) {
// /* Check if all agents have reached their goals. */
// for (let i = 0; i < num; i++) {
// if (i == remove_id && is_remove) {
// continue;
// }
// let position = sim.get_agent_position(i);
// if (Vector2.abs_sq(goals[i].sub(sim.get_agent_position(agents[i]))) > 2) {
// return false;
// }
// }
// return true;
// }
init().then(module => {
// 在这里调用 Rust 函数
// console.log(module)
let sim = RVOSimulator.default();
let goals = [];
let divs = [];
let agents = [];
setup_scenario(sim, goals, divs, agents);
let begin = performance.now();
let r = 0;
let id = setInterval(() => {
let begin = performance.now();
if (update_visualization(sim, divs, agents)) {
clearInterval(id);
}
let res = sim.do_step();
// console.log("======= time: ", performance.now() - begin, " res: " + res);
}, 16)
});
</script>
</body>
</html>